package com.hexiang.error;

import com.hexiang.ratelimit.RateLimitException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * Let Spring handle the exception, we just override the status code
     *
     * @param response
     * @throws IOException
     */
    @ExceptionHandler(RateLimitException.class)
    public void rateLimit(RateLimitException e,HttpServletResponse response) throws IOException {
        log.error("overRateLimit:{}",e.toString());
        response.sendError(HttpStatus.TOO_MANY_REQUESTS.value());
    }

    @ExceptionHandler(Exception.class)
    public void internalServerError(HttpServletResponse response) throws IOException {
        response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

}
